<?php

/**
 *
 * A very simple controller that allows
 * an user to change his password.
 * @author Théo Chamley
 *
 */
class GestionpersoController extends CodeBar_AbstractController {

	public function init()
	{
		CodeBar_AbstractController::init();
		if(!($this->session()->user->connected))
		{
			$this->_helper->redirector('index');
		}
		else if($this->session()->permissionactive != "GestionPerso")
		{
			$this->_helper->redirector('PermissionChoice','index');
		}
	}

	/**
	 * (non-PHPdoc)
	 * Allows a user to change his password.
	 * @see trunk/library/CodeBar/CodeBar_AbstractController::indexAction()
	 */
	public function indexAction() {
		$this->view->headTitle("Changement de password");
		/*
		 * JS
		 */
		$this->view->headScript()->appendFile('/js/GestionPerso.js','text/javascript');

		/*
		 * CSS
		 */
		$this->view->headLink()->appendStylesheet('/css/stylesheet_mdp.css');

		$this->view->user = $this->session()->user;

		$form=new Application_Form_GestionPerso_ChangePassForm();
		$this->view->form=$form;
		if($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)) {
				if(md5($form->getValue('pwd_old')) == $this->session()->user->getPwd()) {
					if ($form->getValue('pwd_new') == $form->getValue('pwd_verif') && mb_strlen($form->getValue('pwd_new'))>=6) {
						$pwd=md5($form->getValue('pwd_new'));
						$this->session()->user = $this->session()->user->changePwd($pwd);
						$this->redirect('modulechoice', 'index');
						
					} else if(mb_strlen($form->getValue('pwd_new'))>=6) {
						$this->view->error = "Les mots de passe ne correspondent pas";
						
					} else {
						$this->view->error = "Le mot de passe fait moins de 6 caractères";
					}
				} else {
					$this->view->error= "Mot de passe incorrect";
				}
			}
		}
	}
}